const Router = require('koa-router');
const router = new Router();
const {db, QueryTypes} = require("../../models/db");

// 选中商户，加载历史送货员，销售员
router.post('/bill/getHistSales', async ctx => {
    let user = ctx.user
    let body = ctx.request.body
    let tenantId = user.tenantId;
    let buId = body.buId;
    if (!buId) {
        return ctx.suc('', {})
    }
    let sql = `
        select top 1 a.deliverIds,a.deliverNames,a.salesId,isnull(b.name,'') salesName from invoice a left join admin b on a.salesId=b.id
        where a.tenantId=${tenantId} and a.billType='SALE' and isnull(a.salesId,0)<>0 and a.buId=${buId} order by a.id desc
    `
    let ret = await db.query(sql, { type: QueryTypes.SELECT, plain: true })

    return ctx.suc('', ret);
})

module.exports = router;